package cn.tuling.v3;

public class MementoTest {
    public static void main(String[] args) {
        Boy boy = new Boy();
        boy.setState("You good good.");
        System.out.println("初始状态");
        System.out.println(boy.getState());

        Caretaker caretaker = new Caretaker();
        caretaker.setMemento(boy.createMemento());
        boy.changeState();
        System.out.println("改变状态");
        System.out.println(boy.getState());

        System.out.println("恢复状态");
        boy.restoreMemento(caretaker.getMemento());
        System.out.println(boy.getState());
    }
}

class Memento {
    private String state;

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }
}

class Boy {
    private String state;

    public void changeState() {
        this.state = "You so bad bad.";
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public Memento createMemento() {
        Memento memento = new Memento();
        memento.setState(state);
        return memento;
    }

    public void restoreMemento(Memento backupMemento) {
        this.setState(backupMemento.getState());
    }
}

class Caretaker {
    private Memento memento;

    public Memento getMemento() {
        return memento;
    }

    public void setMemento(Memento memento) {
        this.memento = memento;
    }
}
